package com.example.gray.entity;

import com.example.gray.service.OpLogService;
import com.mysql.fabric.xmlrpc.base.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Gray 2022/1/4-9:59
 */
//@Component
public class Schedule {
    @Autowired
    OpLogService service;

    @Async
    @Scheduled(fixedRate = 10000)
    public void run(){
        Date myDate = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = format.format(myDate);
        OpLog log = new OpLog(0,"192.168.50.31",11,date,"系统每隔10s自动输入");
        service.insert(log);
        System.out.print(format.format(myDate)+"-插入一次\n");
    }

    @Async
    @Scheduled(fixedRate = 5000)
    public void select(){
        List<OpLog> logs = service.selectAll();
        System.out.println("总条数："+logs.size());
    }

    @Async
    @Scheduled(fixedRate = 5000)
    public void selectNew10(){
        List<OpLog> logs = service.selectNew10();
        System.out.println("最新10条数据：");
        for (OpLog log :
                logs) {
            System.out.println(log.toString());
        }
    }
}
